package com.gcd.alienandwheel.stage;

import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.actors.Image;
import com.badlogic.gdx.scenes.scene2d.actors.Label;
import com.gcd.alienandwheel.MyGame;
import com.gcd.alienandwheel.MyUtil;
import com.gcd.alienandwheel.Vector2;
import com.gcd.alienandwheel.actor.Tyro;
import com.gcd.alienandwheel.actor.Track2;

/**
 *
 * @author Rezan Achmad
 */
public class Arena2 extends MyStage {

    private Tyro tyro;
    private Track2 track;
    private Label carPos;
    private Group group;
    private Image background;
    private float lastX = 0;
    private float lastY = 0;

    public Arena2(MyGame myGame, float width, float height, boolean stretch) {
        super(myGame, width, height, stretch);
        init();
    }

    @Override
    public boolean touchDown(int x, int y, int pointer, int newParam) {
        Vector2 out = new Vector2();
        toStageCoordinates(x, y, out);

        lastX = out.x;
        lastY = out.y;

        return super.touchDown(x, y, pointer, newParam);
    }

    @Override
    public boolean touchUp(int x, int y, int pointer, int button) {
        Vector2 out = new Vector2();
        toStageCoordinates(x, y, out);

        Vector2 v = new Vector2(out.x - lastX, out.y - lastY);
        float angle = v.angle();
        tyro.speedForward += v.y;
        System.out.println("dx : " + v.x);
        System.out.println("dy : " + v.y);
        System.out.println("Angle : " + angle);

        if (angle == 0) {
            // Sumbu x+
        } else if (angle < 90) {
            // Kuadran I
        } else if (angle == 90) {
            // Sumbu y+
        } else if (angle < 180) {
            // Kuadran II
        } else if (angle == 180) {
            // Sumbu x-
        } else if (angle < 270) {
            // Kuadran III
        } else if (angle == 270) {
            // Sumbu y-
        } else if (angle < 360) {
            // Kuadran IV
        }

        return super.touchUp(x, y, pointer, button);
    }

    private void init() {
        background = new Image("background", MyUtil.makeBackgroundColor((int) width, (int) height, new Color(0, 0, 0, 1)));

        group = new Group("group");
        group.x = 0;
        group.y = 0;

        tyro = new Tyro("car");
        tyro.x = width / 2;
        tyro.y = height / 2;

        track = new Track2("track");

        carPos = new Label("carPos", font, "car pos : ");
        carPos.x = 0;
        carPos.y = 20;

        addActor(background);
        addActor(group);
        group.addActor(track);
        group.addActor(tyro);
        addActor(carPos);
    }

    @Override
    public void act(float delta) {
        super.act(delta);

        tyro.onTheLand = isCarOnTheLand();

        if (input.isKeyPressed(Keys.KEYCODE_W)) {
            tyro.speedForward += (tyro.acc * delta);
        }

        if (input.isKeyPressed(Keys.KEYCODE_S)) {
            tyro.speedForward -= (tyro.acc * delta);
        }

        if (input.isKeyPressed(Keys.KEYCODE_A)) {
            tyro.rotation += (tyro.rotInc * tyro.speedForward / tyro.maxSpeed) * delta;
        }

        if (input.isKeyPressed(Keys.KEYCODE_D)) {
            tyro.rotation -= (tyro.rotInc * tyro.speedForward / tyro.maxSpeed) * delta;
        }

        // Update group
        group.rotation = -tyro.rotation;
        group.originX = tyro.x + tyro.width / 2;
        group.originY = tyro.y + tyro.height / 2;
        group.x = width / 2 - group.originX;
        group.y = height / 2 - group.originY;

        carPos.text = "car speed : " + tyro.speedForward;
    }

    private boolean isCarOnTheLand() {
        Pixmap pixmapTrack = track.pixmapMapModel;
        Pixmap pixmapCar = tyro.wheel.pixmapTexture;
        int pixel = 0;
        Vector2 v;

        for (float i = 0; i < tyro.width; ++i) {
            for (float j = 0; j < tyro.height; ++j) {

                if (pixmapCar.getPixel((int) i, (int) j) % 256 == 0) {
                    continue;
                }

                v = new Vector2(i - tyro.originX, j - tyro.originY);
                v.rotate(tyro.rotation);
                pixel = pixmapTrack.getPixel((int) (tyro.x + v.x + tyro.originX), (int) (pixmapTrack.getHeight() - tyro.y - v.y - tyro.originY));
                if (pixel == 255) {
                    return true;
                }
            }
        }

        return false;
    }
}
